// pages/ucenter/vipRecharge/vipRecharge.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    vipType: 1,
    plateType: 1,
    addLicensePlates: [],
    plateIds: [],
    defaultPlate: '',
    defaultId: 0,
    searchImg:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  togo(){
    wx.navigateTo({
      url: '/pages/mrecharge/index',
    })
  },
  search(e){
    if(e.detail.value.length>0){
      this.setData({
        searchImg:false
      })
    }else{
      this.setData({
        searchImg:true
      })
    }
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getPlates();
  },

  back() {
    wx.navigateBack()
  },

  checkPlate(){
    let that=this;
    if(this.data.plateType==1 && this.data.addLicensePlates.length==0){
      wx.showModal({
        content: '您还未绑定车牌号，是否前往绑定？',
        success(res){
          if(res.confirm){
            wx.navigateTo({
              url: '/pages/ucenter/addLicensePlate/index',
            })
          }else{
            that.setData({
              plateType:2
            })
          }
        }
      })
    }else{
      wx.hideToast()
    }
  },

  changeType(e) {
    this.setData({
      vipType: e.currentTarget.dataset.type
    })
  },

  changePlateType(e) {
    console.log(e, '选择项')
    this.setData({
      plateType: e.detail.value
    })
    this.addcar = this.selectComponent("#addcar");
  },

  getPlates() {
    let that = this;
    var userId = wx.getStorageSync('userId');
    wx.request({
      //车辆查询
      url: app.globalData.basicUrl + 'user/userPlate/userId/' + userId,
      // data:1,
      success: function (res) {
        console.log(res);
        let addLicensePlates = [],
          plateIds = []
          if(res.data.data.length==0){
            that.checkPlate()
          }else{
            res.data.data.map(item => {
              addLicensePlates.push(item.plateNo)
              plateIds.push(item.plateId)
              that.setData({
                addLicensePlates: addLicensePlates,
                plateIds: plateIds,
                defaultPlate: res.data.data[0].plateNo
              })
            })
          }
      }
    })
  },

  selectPlate(e) {
    let that = this;
    this.setData({
      defaultPlate: that.data.addLicensePlates[e.detail.value],
      defaultId: that.data.plateIds[e.detail.value]
    })
  },

  addVip() {
    let that = this,
      data = {
        money: that.data.vipType == 1 ? '360' : '120',
        openId: app.globalData.openId,
        plateId: '',
        plateNo: '',
        userId: Number(app.globalData.userId),
        vipType: that.data.vipType,
        flag:'开通会员'
      };
    console.log(this.addcar, '组件加载')
    if (that.data.plateType == 1) {
      console.log(that.data.defaultPlate, '车牌')
      data.plateId = that.data.defaultId
      data.plateNo = that.data.defaultPlate
    } else {
      var card = this.addcar.data.inputPlates
      var pate = card.index0 + card.index1 + card.index2 + card.index3 + card.index4 + card.index5 + card.index6 + card.index7
      console.log(pate, '车牌')
      delete data.plateId
      data.plateNo = pate
    }
    console.log(data.plateNo,'车牌号')
    let xreg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF]$)|([DF][A-HJ-NP-Z0-9][0-9]{4}$))/,
      creg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/;
    if(!xreg.test(data.plateNo) && !creg.test(data.plateNo)){
      wx.showToast({
        icon:'error',
        title: '车牌号格式错误',
      })
    }else{
      wx.request({
        url: app.globalData.basicUrl + 'user/WxRecharge/recharge',
        method: "POST",
        data: data,
        success(res) {
          console.log(res)
          if(res.statusCode==200){
            that.payVip(res.data)
          }
        },
        fail(res) {
  
        }
      })
    }
  },

  payVip(data) {
    wx.requestPayment({
      ...data,
      success(res) {
        wx.showToast({
          title: '支付成功',
          duration: 2000,
          success: function() {
            setTimeout(() => {
              wx.navigateBack()
            }, 2000)
          }
        })
      },
      fail(res){
        wx.showToast({
          title: '支付未成功',
          icon:'error',
          duration: 2000
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})